package org.nnxy.foodboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.nnxy.foodboot.entity.Food;
import org.nnxy.foodboot.entity.FoodCategory1;
import org.nnxy.foodboot.entity.FoodCategory2;
import org.nnxy.foodboot.service.FoodCategory1Service;
import org.nnxy.foodboot.service.FoodCategory2Service;
import org.nnxy.foodboot.service.FoodService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 食材控制器
 */
@RestController
@RequestMapping("/api/food")
@RequiredArgsConstructor
public class FoodController {
    
    private final FoodService foodService;
    private final FoodCategory1Service category1Service;
    private final FoodCategory2Service category2Service;
    
    /**
     * 分页查询食材列表
     */
    @GetMapping("/page")
    public Map<String, Object> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer category1Id,
            @RequestParam(required = false) Integer category2Id) {
        
        // 构建查询条件
        LambdaQueryWrapper<Food> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            queryWrapper.like(Food::getName, name);
        }
        if (category2Id != null) {
            queryWrapper.eq(Food::getCategory2Id, category2Id);
        } else if (category1Id != null) {
            // 如果提供了一级分类ID，查询该一级分类下所有二级分类的食材
            List<FoodCategory2> category2List = category2Service.list(
                    new LambdaQueryWrapper<FoodCategory2>()
                            .eq(FoodCategory2::getCategory1Id, category1Id)
            );
            List<Integer> category2Ids = category2List.stream().map(FoodCategory2::getId).toList();
            if (!category2Ids.isEmpty()) {
                queryWrapper.in(Food::getCategory2Id, category2Ids);
            }
        }

        // 分页查询
        Page<Food> page = foodService.page(new Page<>(current, size), queryWrapper);

        // 查询每个食材的分类信息
        for (Food food : page.getRecords()) {
            FoodCategory2 category2 = category2Service.getById(food.getCategory2Id());
            if (category2 != null) {
                food.setFoodCategory2(category2);

                FoodCategory1 category1 = category1Service.getById(category2.getCategory1Id());
                category2.setFoodCategory1(category1);
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", page);
        return result;
    }
    
    /**
     * 获取食材详情
     */
    @GetMapping("/{id}")
    public Map<String, Object> getById(@PathVariable Integer id) {
        Food food = foodService.getById(id);
        
        if (food != null) {
            FoodCategory2 category2 = category2Service.getById(food.getCategory2Id());
            if (category2 != null) {
                food.setFoodCategory2(category2);
                
                FoodCategory1 category1 = category1Service.getById(category2.getCategory1Id());
                category2.setFoodCategory1(category1);
            }
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", food);
        return result;
    }
    
    /**
     * 新增食材
     */
    @PostMapping
    public Map<String, Object> save(@RequestBody Food food) {
        boolean success = foodService.save(food);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "添加成功");
        } else {
            result.put("code", 500);
            result.put("message", "添加失败");
        }
        return result;
    }
    
    /**
     * 修改食材
     */
    @PutMapping
    public Map<String, Object> update(@RequestBody Food food) {
        boolean success = foodService.updateById(food);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "修改成功");
        } else {
            result.put("code", 500);
            result.put("message", "修改失败");
        }
        return result;
    }
    
    /**
     * 删除食材
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> remove(@PathVariable Integer id) {
        boolean success = foodService.removeById(id);
        
        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }
        return result;
    }
} 